﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Helpers;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight;
using System.Collections.Generic;
using System;
using TestApp.Model;
using TestApp.Model.Managers;
using TestApp.Navigation;
using TestApp.Messages;
using TestApp.Resources;
using TestApp.ViewModel.ItemsViewModel;

namespace TestApp.ViewModel
{
    public class MoreViewModel : ViewModelBase, IHomeBaseViewModel, IDynamicDataTemplate
    {
        // Property Names
        private const string SelectedIndexProperty = "SelectedIndex";

        // Navigation Service
        private INavigationService NavigationService;
        
        // Uri sender
        private MessageSender<UriInitMessage> UriSender;


        // Title
        public  String Title
        {
            get
            {
                return LocalizedStrings.MorePivot;
            }
        }

        // Data Template
        public  String DataTemplate
        {
            get
            {
                return @"MoreViewTemplate";
            }
        }

        // Items
        public List<MoreItemViewModel> ItemsList
        {
            get;
            set;
        }

        // Selection Changed Command
        public RelayCommand<Int32> OnSelectionChanged
        {
            get;
            private set;
        }

        // Selected Items
        public Int32 SelectedIndex
        {
            get
            {
                return -1;
            }
        }

        // Constructor
        public MoreViewModel(INavigationService navigationService)
        {
            OnSelectionChanged = new RelayCommand<Int32>((c) => SelectionChanged(c));
            NavigationService = navigationService;

            // Item titles
            ItemsList = new List<MoreItemViewModel>() {
                new StoreRadiusItemViewModel() {
                    Title = LocalizedStrings.More_StoreRadius,
                    DataTemplate = @"MoreItemViewStoreRadiusTemplate"
                },
                new MoreItemViewModel() {
                    Title = LocalizedStrings.More_About,
                    DataTemplate = @"MoreItemViewRegularTemplate"
                },
                new MoreItemViewModel() {
                    Title = LocalizedStrings.More_WriteAReview,
                    DataTemplate = @"MoreItemViewRegularTemplate"
                },
                new MoreItemViewModel() {
                    Title = LocalizedStrings.More_TellAFriend,
                    DataTemplate = @"MoreItemViewRegularTemplate"
                },
                new MoreItemViewModel() {
                    Title = LocalizedStrings.More_Facebook,
                    DataTemplate = @"MoreItemViewRegularTemplate"
                },
                new MoreItemViewModel() {
                    Title = LocalizedStrings.More_Twittter,
                    DataTemplate = @"MoreItemViewRegularTemplate"
                }
            };
        }

        // Selection Changed
        private void SelectionChanged(Int32 selectedIndex)
        {
            if (selectedIndex != -1)
            {
                RaisePropertyChanged(SelectedIndexProperty);
                if (selectedIndex == 4)
                {
                    // Facebook URI
                    if (UriSender == null)
                    {
                        UriSender = new MessageSender<UriInitMessage>();
                    }
                    UriSender.SendMessage(new UriInitMessage(new Uri(@"http://m.facebook.com/shoprite.supermarket/?v=wall", UriKind.Absolute)));
                    NavigationService.NavigateTo(ViewModelLocator.WebBrowserPage);
                }
            }
        }
    }
}
